package com.fsm.system.serviceImpl;

import com.fsm.common.tools.Result.Result;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.util.StringUtil;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.fsm.system.entity.TSystemTenant;
import com.fsm.system.mapper.TSystemTenantMapper;
import com.fsm.system.service.TSystemTenantService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

/**
 *  租户管理服务层实现。
 *
 * @author
 * @since
 */
@Service
public class TSystemTenantServiceImpl extends ServiceImpl<TSystemTenantMapper, TSystemTenant> implements TSystemTenantService {

    /**
     * 公共整合查询条件
     * @param tSystemTenant
     * @return
     */
    private static QueryWrapper getSelectQueryWrapper(TSystemTenant tSystemTenant){
        QueryWrapper queryWrapper = new QueryWrapper();
        return queryWrapper;
    }

    /**
     * 添加租户管理
     *
     * @param tSystemTenant
     * @return
     */
    @Override
    @Transactional
    public Result addTSystemTenant(TSystemTenant tSystemTenant) throws Exception {
        int sign = this.getMapper().insert(tSystemTenant);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("添加租户管理失败");
        }
        return Result.ok();
    }

    /**
     * 删除租户管理
     *
     * @param tSystemTenant
     * @return
     */
    @Transactional
    @Override
    public Result deleteTSystemTenant(TSystemTenant tSystemTenant) throws Exception {
        int sign = this.getMapper().deleteById(tSystemTenant.getId());
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("删除租户管理失败");
        }
        return Result.ok();
    }

    /**
     * 更新租户管理
     *
     * @param tSystemTenant
     * @return
     */
    @Transactional
    @Override
    public Result updateTSystemTenant(TSystemTenant tSystemTenant) throws Exception {
        int sign = this.getMapper().update(tSystemTenant);
        if(sign == 0){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.fail("更新租户管理失败");
        }
        return Result.ok();
    }

    /**
     * 分页查询租户管理
     *
     * @param tSystemTenant
     * @return
     */
    @Override
    public Result pageTSystemTenant(TSystemTenant tSystemTenant) throws Exception {
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemTenant);
        if(tSystemTenant.getPage() != null && tSystemTenant.getLimit() != null){
            Page<TSystemTenant> tSystemTenantPage = this.getMapper().paginateWithRelations(tSystemTenant.getPage(),tSystemTenant.getLimit(),queryWrapper);
            return Result.ok(tSystemTenantPage.getRecords(),tSystemTenantPage.getTotalRow());
        }else{
            List<TSystemTenant> tSystemTenantList = this.getMapper().selectListWithRelationsByQuery(queryWrapper);
            return Result.ok(tSystemTenantList,tSystemTenantList.size());
        }
    }

    /**
     * 查询租户管理
     *
     * @param tSystemTenant
     * @return
     */
    @Override
    public Result getTSystemTenantList(TSystemTenant tSystemTenant) throws Exception{
        QueryWrapper queryWrapper = getSelectQueryWrapper(tSystemTenant);
        List<TSystemTenant> tSystemTenantList = this.getMapper().selectListWithRelationsByQuery(queryWrapper);
        return Result.ok(tSystemTenantList,tSystemTenantList.size());
    }

    /**
     * 通过ID查询租户管理
     *
     * @param tSystemTenant
     * @return
     */
    @Override
    public Result getTSystemTenantById(TSystemTenant tSystemTenant) throws Exception{
        TSystemTenant tSystemTenantOne = this.getMapper().selectOneWithRelationsById(tSystemTenant.getId());
        return Result.ok(tSystemTenantOne);
    }

}
